package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.service.CustomerService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerService customerService;

    /**
     * 获取业务列表
     */
    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {

        Page<Business> page = businessMapper.listBusiness(new Page<Business>(businessDto.getPage(),
                                    businessDto.getPageSize()), businessDto);

        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    /**
     * 根据id查询商机
     */
    @Override
    public Business selectById(Integer id) {
        return businessMapper.selectBusinessById(id);
    }

    /**
     * 跟进商机
     * 1.根据id更新商机基本信息
     * 2.添加商机跟进记录
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackBusiness(Business business) {
        // 更新商机信息
        business.setStatus(3);//跟进中
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        // 添加商机跟进记录 插入一条数据到商机跟进记录表
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());//商机id
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());//跟进
        //businessTrackRecord.setKeyItems(business.getKeyItems().toString());//沟通重点
        businessTrackRecord.setKeyItems(String.join(",",business.getKeyItems()));//沟通重点
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());//沟通纪要
        businessTrackRecord.setCreateTime(LocalDateTime.now());

        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    /**
     * 踢回公海池
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void backPool(Integer id) {
        // 1.更新商机信息
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);//回收
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

/*
        // 2.添加商机跟进记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(id);
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(3);
        businessTrackRecord.setCreateTime(LocalDateTime.now());
//        businessTrackRecord.setRecord("回公海");

        businessTrackRecordMapper.insert(businessTrackRecord);
*/
    }

    /**
     * 公海列表
     */
    @Override
    public PageResult<Business> poolList(BusinessDto businessDto) {
        Page<Business> page = businessMapper.poolList(new Page<Business>(businessDto.getPage(),
                businessDto.getPageSize()), businessDto);

        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    /**
     * 转客户
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toCustomer(Integer id) {
        //1.更新商机状态为转客户
        Business business = businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        this.updateById(business);

        //2.给客户表插入一条数据
        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);
        customer.setId( null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());

        //3.调用方法，插入一条数据到客户表
        customerService.save(customer);

    }
}
